package com.xlf.service.base.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.xlf.common.core.constant.Constants;
import com.xlf.common.core.exception.BizException;
import com.xlf.common.core.util.StringUtils;
import com.xlf.common.core.util.page.PageUtils;
import com.xlf.common.core.util.page.TableDataInfo;
import com.xlf.pojo.base.Announcement;
import com.xlf.pojo.base.Tag;
import com.xlf.service.base.pojo.vo.AnnouncementListVO;
import com.xlf.service.base.service.AnnouncementService;
import com.xlf.service.base.mapper.AnnouncementMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 小新
* @description 针对表【announcement】的数据库操作Service实现
* @createDate 2023-12-31 11:17:20
*/
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement>
    implements AnnouncementService{

    @Override
    public TableDataInfo<AnnouncementListVO> getCommonAnnouncement() {

        PageUtils.startPage();
        List<Announcement> list = lambdaQuery()
                .select(Announcement::getId,Announcement::getTitle,
                        Announcement::getUrl, Announcement::getCreateTime,Announcement::getSeq)
                .eq(Announcement::getStatus, Constants.NORMAL)
                .orderByDesc(Announcement::getSeq,Announcement::getId)
                .list();
        if(StringUtils.isEmpty(list))
            PageUtils.getEmptyDataTable();

        List<AnnouncementListVO> announcementListVOS = BeanUtil.copyToList(list, AnnouncementListVO.class);

        return PageUtils.getDataTable(announcementListVOS,new PageInfo<>(list).getTotal());
    }

    @Override
    public Announcement getAnnouncement(Long id) {

        Announcement one = lambdaQuery()
                .eq(Announcement::getStatus, Constants.NORMAL)
                .eq(Announcement::getId, id)
                .orderByDesc(Announcement::getSeq, Announcement::getId)
                .one();
        if(one == null)
            throw new BizException("公告不存在！");

        return one;
    }

    @Override
    public List<Announcement> getList(Announcement announcement) {

        return lambdaQuery()
                .eq(StringUtils.isNotNull(announcement.getStatus()),Announcement::getStatus,announcement.getStatus())
                .like(StringUtils.isNotEmpty(announcement.getTitle()),Announcement::getTitle,announcement.getTitle())
                .between(StringUtils.isNotEmpty(announcement.getParams()) &&
                                announcement.getParams().get("beginTime") != null && announcement.getParams().get("endTime") != null,
                        Announcement::getCreateTime, announcement.getParams().get("beginTime"),
                        announcement.getParams().get("endTime")).list();
    }

    @Override
    public boolean updateStatus(Announcement announcement) {
        return lambdaUpdate().set(Announcement::getStatus,announcement.getStatus()).eq(Announcement::getId,announcement.getId()).update();
    }
}




